<template>
  <div class="common-layout" :style="`width: ${state.width}`">
  <el-main>
    <!-- 数据未准备好时显示加载状态 -->
    <div v-if="!state.dataReady" class="loading">加载中...</div>

    <!-- 数据准备好后渲染实际内容 -->
    <div v-else>
      总量：{{ state.rawData.length }}
      数量：{{ state.stocks.length }}
      <ul v-infinite-scroll="load" class="infinite-list" style="overflow: auto">
        <li v-for="i in count" :key="i" class="infinite-list-item">
          {{ i }}
            <to-stock-detail :stock="state.stocks[i - 1]"></to-stock-detail>

            <el-tabs v-model="state.activeName[i-1]" class="demo-tabs">
              <el-tab-pane label="日线" name="daily">
                <mean-daily-close-echart-refine :stock="state.stocks[i - 1]" v-if="state.activeName[i-1] === 'daily'"></mean-daily-close-echart-refine>
              </el-tab-pane>
              <el-tab-pane label="周线" name="week">
                <mean300d-close-echart-refine  :stock="state.stocks[i - 1]" v-if="state.activeName[i-1] === 'week'"></mean300d-close-echart-refine>
              </el-tab-pane>
              <el-tab-pane label="操作" name="op">
                <LastedOperation  :stock="state.stocks[i - 1]" v-if="state.activeName[i-1] === 'op'"></LastedOperation>
              </el-tab-pane>
              <el-tab-pane label="日统计" name="日统计">
                <TradingRankRefine  :stock="state.stocks[i - 1]" v-if="state.activeName[i-1] === '日统计'" />
              </el-tab-pane>
            </el-tabs>
        </li>
      </ul>
    </div>
  </el-main>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import Mean300dCloseEchartRefine from '../Mean300dCloseEchartRefine.vue'
import MeanDailyCloseEchartRefine from '../MeanDailyCloseEchartRefine.vue'
import TradingRankRefine from '../TradingRankRefine.vue'
import ToStockDetail from '../ToStockDetail.vue'
import { getRollStocks } from '../../service/stock'
import LastedOperation from '../operation/LastedOperation.vue'
import { useRouteQuery } from '../../utils/useRoute'

document.title = 'roll'
const activeNameKey = "Roll"

const count = ref(0)
const state = reactive({
  width: '100%',
    dataReady: false,
    rawData: [],
    stocks: [],
    search: '',
    echartType: 'daily',
    activeName: [],
})

state.width = window.screen.width - 50 * 'px'

state.echartType = useRouteQuery('echartType',"week")
state.search = useRouteQuery('search',"股票")

let page = useRouteQuery('p',1)
let pageSize = useRouteQuery('ps',50)

const getMStocks = () => {
  getRollStocks(state.search).then(res => {
    state.dataReady = false
    count.value = 0

    state.rawData = res.data
    state.stocks = res.data.slice((page - 1) * pageSize, (page) * pageSize)
    state.stocks.forEach(item => {
      state.activeName.push(state.echartType)
    })

    state.dataReady = true
  })
}

const load = () => {
  if (count.value < state.stocks.length)
      count.value += Math.min(2, state.stocks.length - count.value)
}

getMStocks()
</script>

<style>
.infinite-list {
  height: 900px;
  padding: 0;
  margin: 0;
  list-style: none;
}
.infinite-list .infinite-list-item {
  display: flex;
  align-items: center;
  justify-content: center;
  /* height: 600px; */
  background: var(--el-color-primary-light-9);
  margin: 10px;
  color: var(--el-color-primary);
}
.infinite-list .infinite-list-item + .list-item {
  margin-top: 10px;
}
</style>